package com.dq.controller.op.opmerchant;

import com.dq.annotation.aop.DataFilter;
import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.controller.op.opmerchant.in.query.AttributesQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.category.Attributes;
import com.dq.domain.org.Org;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.dq.validator.ValidatorUtils;
import com.dq.validator.group.AddGroup;
import com.dq.validator.group.UpdateGroup;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author gewenzhen
 */
@Api(description = "商品属性管理")
@RestController
@RequestMapping("/attributes")
public class AttributesController extends AllService {
    @ApiOperation("所有商品属性列表")
    @PostMapping("/list")
    @RequiresPermissions("attributesList:list")
    @DataFilter(deptAlias = "org")
    public Vo list(@RequestBody AttributesQuery attributesQuery) throws AdminException {
        Map<String, Object> query = attributesQuery.parseMap();
        List<Attributes> list = dao.find(dao.queryAdminHql(Attributes.class, query), query);
        attributesQuery.format();
        PageHelper.startPage(attributesQuery.getPage(), attributesQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<Attributes.Vo>) VO.toVOCollection(list));
        pageInfo.setTotal(dao.count(dao.countAdminHql(Attributes.class, query), query));
        return new Vo(pageInfo);
    }

    @ApiOperation("保存属性")
    @PostMapping("/save")
    @RequiresPermissions("attributesList:save")
    public Vo save(@RequestBody Attributes attributes) throws AdminException {
        ValidatorUtils.validateEntity(attributes, AddGroup.class);
        attributes.setCreateUser(shiroService.getUser());
        attributes.setOrg(dao.get(Org.class, shiroService.getDeptId()));
        return new Vo(attributesRepository.save(attributes).toVO(false));
    }

    @ApiOperation("修改属性")
    @PostMapping("/update/{attributesId}")
    @RequiresPermissions("attributesList:update")
    public Vo update(@RequestBody Attributes updateAttributes, @PathVariable @ApiParam(value = "属性id") Long
            attributesId) throws AdminException {
        ValidatorUtils.validateEntity(updateAttributes, UpdateGroup.class);

        Attributes attributes = dao.get(Attributes.class, attributesId);
        if (attributes == null) {
            throw new AdminException("属性不存在");
        }
        attributes.setUpdateUser(shiroService.getUser());
        BeanUtils.copyProperties(updateAttributes, attributes, BeanNullUtil.getNullPropertyNames(updateAttributes));
        attributesRepository.save(attributes);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除属性")
    @SysLog("删除属性")
    @PostMapping("/delete/{attributesId}")
    @RequiresPermissions("attributesList:del")
    public Vo delete(@PathVariable @ApiParam(value = "属性id") Long attributesId) throws AdminException {
        Attributes attributes = dao.get(Attributes.class, attributesId);
        if (null == attributes) {
            throw new AdminException("属性不存在");
        }
        attributes.setDeleted(true);
        attributesRepository.save(attributes);
        return new Vo(HttpStatus.OK);
    }
}
